﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using App.Data;
using App.Core.Extensions;

public partial class UserControls_CustomerList : BaseUserControl
{
    public List<Customer> DataSource { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public override void DataBind()
    {
        BindCustomerList();
        base.DataBind();
    }
    protected void BindCustomerList()
    {
        if (DataSource == null || DataSource.Count == 0)
            ltrListing.Text = String.Empty;
        else
        {
            String listItemHtml = @"<div class='ListItem'>                                    
                                    <div class='ListTitle'><a href='javascript:void();' NavigateUrl='/Pages/Public/CompanyProfile.aspx?ID={0}'>{1}</a></div>
                                    <div class='ListSubTitle'><b>Location:</b> {2}</div>
                                    <div style='margin-top:5px;'>
                                        {3}{4}
                                        <div class='ListBotter'>{5}</div>                
                                    </div>                                    
                                </div>";
            String thumbImage = "<img src='" + AppConstants.CUSTOMER_FILES + "/{0}/{1}' alt='' title='' align='right' class='ThumbInList' />";
            StringBuilder sb = new StringBuilder(10);
            foreach (Customer customer in this.DataSource)
            {
                String priceHtml = String.Empty;
                String thumnail = String.Empty;

                //if (project.PricePerUnitFrom > 0 && project.PricePerUnitTo > 0 & project.ShowPricingInfo)
                //    priceHtml = String.Format("From {0} To {1}", WebUtil.FormatData(project.PricePerUnitFrom), WebUtil.FormatData(project.PricePerUnitTo));
                //else
                //    priceHtml = "Negotiatable";
                String logo = customer.Logo.IsNullOrEmpty() ? String.Empty : String.Format(thumbImage, "Logo", customer.Logo);
                String webSite = customer.WebSite.IsNullOrEmpty() ? String.Empty : String.Format("<b>Web Site: </b><span><a href='{0}' target='_blank'>{1}</a></span>", WebUtil.GetCompleteUrl(customer.WebSite), customer.WebSite);
                sb.AppendFormat(listItemHtml, customer.ID, customer.Name.HtmlEncode(),
                    String.Format("{0} {1}", "London", String.Empty),
                    logo, GetDisplayableCustomerInfo(customer),
                    webSite);
                ltrListing.Text = sb.ToString();
            }            
        }
    }
    protected String GetDisplayableCustomerInfo(Customer customer)
    {
        if (customer.About.WordCount() > ConfigReader.ProjectListDescriptionWordCount)
            return String.Format("{0} &nbsp;&nbsp;<a href='/Pages/Public/CompayProfile.aspx?ID={1}'>Details</a>", WebUtil.FormatText(customer.About.GetWords(ConfigReader.ProjectListDescriptionWordCount)), customer.ID);

        return WebUtil.FormatText(customer.About);

    } 
}